import requests
import json
import PackageAnalyse.PackageAnalyseConfig as Pg
import csv


class PackageAnalyseTool:
    @staticmethod
    def get_url_asset_data(full_url: str) -> dict:
        """
        获取对应地址内容
        :param full_url: 资源路径地址
        :return:
        """
        return requests.get(full_url).json()

    @staticmethod
    def get_local_file_asset_data(path: str) -> dict:
        """
        获取本地数据文件内容
        :param path: 文件地址
        :return:
        """
        return PackageAnalyseTool.str2json(PackageAnalyseTool.load_content_in_txt(path, Pg.FileReadMode.s.value))

    @staticmethod
    def str2json(content: str):
        """
        字符串转为字典
        :param content: 待转换内容
        :return:
        """
        return json.loads(content)

    @staticmethod
    def json2str(content: dict):
        """
        字典转为字符串
        :param content: 待转换内容
        :return:
        """
        return json.dumps(content)

    @staticmethod
    def load_content_in_txt(path: str, mode: str):
        """
        加载本地txt文件
        :param path: 文件路径
        :param mode: 返回数据结构
        :return:
        """
        if mode == Pg.FileReadMode.s.value:
            with open(path, 'r', encoding='utf-8') as f:
                return f.read()
        elif mode == Pg.FileReadMode.l.value:
            with open(path, 'r', encoding='utf-8') as f:
                return f.readlines()

    @staticmethod
    def write_to_txt(data, path: str) -> None:
        """
        写入本地txt文件
        :param data: 写入数据
        :param path: 写入文件地址
        :return:
        """
        with open(path, 'w', encoding='utf-8') as f:
            f.write(data)

    @staticmethod
    def get_asset_data_in_asset_list(content: dict, key: str):
        """
        获取文本内容里key值为m_assetList的值
        :param content: 源数据
        :param key: 元素key值
        :return:
        """
        return PackageAnalyseTool.get_asset_path_in_asset_list(content.get(key))

    @staticmethod
    def get_asset_data_in_bundle_list(content: dict, key: str):
        """
        获取文本内容里key值为bundleList的值
        :param content: 源数据
        :param key: 元素key值
        :return:
        """
        return PackageAnalyseTool.get_asset_path_in_bundle_list(content.get(key))

    @staticmethod
    def get_check_asset_data(path: str, key: str):
        """
        获取本地数据文件内容
        :param path: 文件地址
        :param key: 键名
        :return:
        """
        data = PackageAnalyseTool.load_content_in_txt(path, Pg.FileReadMode.l.value)
        return {asset.get(key): asset for asset in [PackageAnalyseTool.str2json(asset) for asset in data]}

    @staticmethod
    def get_asset_path_in_asset_list(asset_list):
        """
        获取asset_list所有资源路径
        :param asset_list: 源数据
        :return:
        """
        return {asset.get(Pg.AssetEleKey.assetPath.value) for asset in asset_list}

    @staticmethod
    def get_asset_path_in_bundle_list(bundle_list):
        """
        获取bundle_list所有资源路径
        :param bundle_list: 源数据
        :return:
        """
        return {asset for bundle in bundle_list for asset in bundle.get(Pg.AssetEleKey.containAssets.value)}

    @staticmethod
    def save_asset_path_to_csv(header, data, path):
        data = [{header[0]: asset} for asset in data]
        PackageAnalyseTool.write_to_csv(header, data, path)

    @staticmethod
    def write_to_csv(header, data, path):
        with open(path, mode='w', newline='', encoding='utf-8') as f:
            writer = csv.DictWriter(f, fieldnames=header)
            writer.writeheader()
            writer.writerows(data)
